using System;
using System.Collections.Generic;
using Yukar.Engine;

namespace Bakin
{
    public class ChangeFieldBattleViewer : BakinObject
    {
        public override void Start()
        {
            // キャラクターが生成される時に、このメソッドがコールされます。
            // This method is called when the character is created.

            mapScene.fieldBattleViewer = new FieldBattleViewer(mapScene, catalog);
        }

        public override void Update()
        {
            base.Update();
        }

        public class FieldBattleViewer : Yukar.Engine.FieldBattleViewer
        {
            private MapScene owner;

            public FieldBattleViewer(MapScene mapScene, Yukar.Common.Catalog catalog) : base(mapScene, catalog)
            {
                owner = mapScene;

                zoomedScale = 4.0f;
                damageScale = 3.0f;
            }

            public override void Draw()
            {
                foreach (var entry in entries)
                {
                    float scale = damageScale;
                    if (entry.totalElapsed < zoomTime)
                    {
                        var t = entry.totalElapsed / zoomTime;
                        scale = t * damageScale + (1 - t) * zoomedScale;
                    }

                    // 破棄されていたら座標更新をストップ
                    if (entry.target?.isDestroyed ?? false)
                    {
                        entry.target = null;
                    }

                    // 座標更新
                    if (entry.target != null)
                    {
                        entry.lastPos = MapScene.GetCharacterPos(entry.target, MapScene.EffectPosType.Body);
                    }
                    MapScene.GetScreenPos(entry.lastPos, out var x, out var y, owner.CurrentPP, owner.CurrentVV);

                    var size = Graphics.MeasureString(0, entry.num.ToString());
                    Graphics.DrawString(0, entry.num.ToString(), new Microsoft.Xna.Framework.Vector2(x, y) + entry.pos - (size * scale / 2),
                        entry.critical ? criticalDamageColor : damageColor, new Microsoft.Xna.Framework.Rectangle(0, 0, Graphics.ScreenWidth, Graphics.ScreenHeight), scale);

                    entry.totalElapsed += GameMain.getElapsedTime();
                }

                entries.RemoveAll(x => x.totalElapsed > drawTime);
            }

            public override void AddDamageDisplay(MapCharacter inTargetMapChr, Microsoft.Xna.Framework.Vector2 vector2, int damage, bool critical = false)
            {
                var entry = new DamageEntry()
                {
                    target = inTargetMapChr,
                    pos = new Microsoft.Xna.Framework.Vector2(owner.GetRandom(16f, -16f), owner.GetRandom(16f, -16f)),
                    num = damage,
                    critical = critical,
                    lastPos = MapScene.GetCharacterPos(inTargetMapChr, MapScene.EffectPosType.Body),
                };
                entries.Add(entry);
            }
        }
    }
}
